package com.meixiang.adapter.myFundAdapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.account.myfund.MyFundActivity;
import com.meixiang.entity.fund.FundOptionalBean;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.MXApplication;
import com.meixiang.recyclerview.ItemTouchHelperAdapter;
import com.meixiang.recyclerview.ItemTouchHelperViewHolder;
import com.meixiang.recyclerview.OnStartDragListener;
import com.meixiang.tool.Tool;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 我的基金 ----自选基金--- 排序
 */
public class FundOptionalSortAdapter extends RecyclerView.Adapter<FundOptionalSortAdapter.ItemViewHolder>
        implements ItemTouchHelperAdapter {

    private final OnStartDragListener mDragStartListener;
    private ArrayList<FundOptionalBean> fundOptionalBeen;
    private static List<String> fund_id_str = new ArrayList<String>();
    public static boolean isDelete = false ;

    public FundOptionalSortAdapter(OnStartDragListener dragStartListener, ArrayList<FundOptionalBean> fundOptionalBeen) {
        mDragStartListener = dragStartListener;
        this.fundOptionalBeen = fundOptionalBeen;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_fund_optional_sort_item, parent, false);
        ItemViewHolder itemViewHolder = new ItemViewHolder(view);
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(final ItemViewHolder holder, final int position) {
        holder.tv_tv_optional_sort_item_number.setText(fundOptionalBeen.get(position).getFund_id());
        holder.tv_optional_sort_item_name.setText(fundOptionalBeen.get(position).getFund_name());
        holder.show_move_ll.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
                    mDragStartListener.onStartDrag(holder);
                }
                return false;
            }
        });

        /**
         * type =2表示默认基金不可删除  0表示加入的自选基金可以删除
         */
        if (!"2".equals(fundOptionalBeen.get(position).getType())) {
            holder.show_delete_iv.setVisibility(View.VISIBLE);
            holder.show_delete_ll.setEnabled(true);
            holder.show_delete_ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    isDelete = true ;
                    fund_id_str.add(fundOptionalBeen.get(position).getFund_id());
                    fundOptionalBeen.remove(position); //移除当前选项
                    notifyItemRemoved(position);
                    notifyDataSetChanged();
                }
            });
        }else{
            holder.show_delete_iv.setVisibility(View.GONE);
            holder.show_delete_ll.setEnabled(false);
        }
    }

    public static List<String> getFundIdStr() {
        return fund_id_str;
    }
    public static void clearFundIdStr(){
        fund_id_str.clear();
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        Collections.swap(fundOptionalBeen, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
        notifyItemChanged(toPosition); //移动之后更新Item的position
        return true;
    }

    @Override
    public int getItemCount() {
        return fundOptionalBeen == null ? 0 : fundOptionalBeen.size();
    }

    public static class ItemViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tv_tv_optional_sort_item_number)
        TextView tv_tv_optional_sort_item_number;
        @Bind(R.id.show_image)
        ImageView handleView;
        @Bind(R.id.show_delete_iv)
        ImageView show_delete_iv;
        @Bind(R.id.tv_optional_sort_item_name)
        TextView tv_optional_sort_item_name;
        @Bind(R.id.show_delete_ll)
        LinearLayout show_delete_ll;
        @Bind(R.id.show_move_ll)
        LinearLayout show_move_ll ;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }
}